//
//  HeroSkinViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HeroSkinViewController.h"
#import "HeroView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "HeroSkinModel.h"

@interface HeroSkinViewController ()
@property(nonatomic, retain)NSMutableArray *imageURL;
@property(nonatomic, retain)NSMutableArray *nameArr;

@end

@implementation HeroSkinViewController
 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.imageURL = [NSMutableArray array];
        self.nameArr = [NSMutableArray array];


    }
    return self;


}
- (void)dealloc
{   [_heroName release];
    [_imageURL release];
    [_nameArr release];
    [super dealloc];
}

// 视图加载完毕
- (void)viewDidLoad {

    [super viewDidLoad];
    [self heroSkinData];


}
- (void)createView
{
    HeroView *heroView = [[HeroView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    [self.view addSubview:heroView];
    [heroView release];
    UIScrollView *heroSkin = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    [heroView addSubview:heroSkin];
    [heroSkin release];
    heroSkin.pagingEnabled = YES;
    heroSkin.bounces = NO;
    heroSkin.backgroundColor = [UIColor blackColor];
    heroSkin.showsHorizontalScrollIndicator = YES;
    heroSkin.contentSize = CGSizeMake(VIEWWIDTH * self.imageURL.count, VIEWHEIGHT);
    for (NSInteger i = 0; i < self.imageURL.count; i++) {

        UIImageView *heroImageView = [[UIImageView alloc] initWithFrame:CGRectMake(VIEWWIDTH  * i , VIEWHEIGHT  / 4.0, VIEWWIDTH, VIEWHEIGHT / 2.5 )];
        //图片自动适应相框(无论图片是长方形,正方形,都只会按照原来的比例显示在相框里)
//        heroImageView.contentMode = UIViewContentModeScaleAspectFill;
//        heroImageView.autoresizesSubviews = YES;
         // 皮肤名称
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(VIEWWIDTH * i , VIEWHEIGHT  / 4.0 - 0.0449775112 * VIEWHEIGHT, VIEWWIDTH, 0.0449775 * VIEWHEIGHT)];
        HeroSkinModel *model = self.nameArr[i];
        nameLabel.text = model.name;
        nameLabel.textAlignment = NSTextAlignmentCenter;

//        nameLabel.alpha = 0.5;
        nameLabel.textColor = [UIColor whiteColor];
        [heroSkin addSubview:nameLabel];
        [nameLabel release];

        // 皮肤价格
        NSString *temp = [NSString stringWithFormat:@"价格:%@点券",model.price];
        UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake( VIEWWIDTH * i, VIEWHEIGHT  / 4.0 + VIEWHEIGHT / 2.5, VIEWWIDTH, 0.0449775 * VIEWHEIGHT)];
        priceLabel.text = temp;

//        priceLabel.alpha = 0.5;
        priceLabel.textAlignment =NSTextAlignmentCenter;
        priceLabel.textColor = [UIColor whiteColor];
        [heroSkin addSubview:priceLabel];
        [priceLabel release];

        [heroImageView sd_setImageWithURL:self.imageURL[i] placeholderImage:[UIImage imageNamed:@"占位图"]];

        [heroSkin addSubview:heroImageView];
        [heroImageView release];


    }



    }
// 网络数据解析
- (void)heroSkinData
{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *temp = [NSString stringWithFormat:@"http://box.dwstatic.com/apiHeroSkin.php?hero=%@&v=70&OSType=iOS7.1.1&versionName=2.2.1",self.heroName];
    [manager GET:temp parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = responseObject;
        for (NSMutableDictionary *dic in arr) {
            NSURL *url = [NSURL URLWithString:dic[@"bigImg"]];
            [self.imageURL addObject:url];
            HeroSkinModel *hero = [[HeroSkinModel alloc] init];
            [hero setValuesForKeysWithDictionary:dic];
            [self.nameArr addObject:hero];
            [hero release];

        }
         [self createView];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求有误");
    }];


}
// 协议必须实现的方法
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
